package cn.tedu.rms.orders.service.impl;

import cn.tedu.rms.cart.mapper.CartMapper;
import cn.tedu.rms.cart.pojo.dto.CartQuery;
import cn.tedu.rms.cart.pojo.vo.CartVO;
import cn.tedu.rms.dish.mapper.DishMapper;
import cn.tedu.rms.dish.pojo.dto.DishQuery;
import cn.tedu.rms.dish.pojo.vo.DishVODetail;
import cn.tedu.rms.dish.pojo.vo.DishVo;
import cn.tedu.rms.orderDetail.mapper.OrderDetailMapper;
import cn.tedu.rms.orderDetail.pojo.entity.OrderDetail;
import cn.tedu.rms.orderDetail.service.OrderDetailService;
import cn.tedu.rms.orders.mapper.OrdersMapper;
import cn.tedu.rms.orders.pojo.dto.OrdersCopyParam;
import cn.tedu.rms.orders.pojo.dto.OrdersQuery;
import cn.tedu.rms.orders.pojo.dto.OrdersSaveParam;
import cn.tedu.rms.orders.pojo.entity.Orders;
import cn.tedu.rms.orders.pojo.vo.OrdersVO;
import cn.tedu.rms.orders.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
@Transactional//保证事务的原子性
public class OrdersServiceImpl implements OrdersService {
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    private OrderDetailService reservationService;
    @Override
    public void insertOrders(OrdersSaveParam ordersSaveParam) {
        log.debug("新增订单表业务:参数={}",ordersSaveParam);
        Orders orders = new Orders();
        BeanUtils.copyProperties(ordersSaveParam,orders);
        //orders.setStatus("0");
        orders.setCreateTime(new Date());
        orders.setRemark("暂无");
        //orders.setPaymentType("0");//后期在支付的方法里添加
        log.debug("orders的数据现在是什么="+orders);

        /**获取菜品总价的方法

        List list = ordersSaveParam.getOrdereddishesList();
        double price = 0;
        for (int i = 0; i <list.size(); i++) {
            //获取菜品单价的方法返回值为price
            price += 获取菜品单价的方法
        }
        //将总价赋值给Total,paymentAmount
        orders.setTotal(price);
        orders.setPaymentAmount(price);

         */

        ordersMapper.insert(orders);
        //需要遍历上述集合，根据集合中的个数 增加对应的订单详情数据
        //需要为其生成对应的订单详情表
        //reservationService.insertReservation(orders);
    }

    @Override
    public List<OrdersVO> selectOrders(OrdersQuery ordersQuery) {
        log.debug("查询业务,用来回显数据");
        List<OrdersVO> list = ordersMapper.selectOrders(ordersQuery);
        log.debug("查询出来的集合结果="+list);
        return list;
    }

    @Override
    public void pay(OrdersSaveParam ordersSaveParam) {
        log.debug("支付业务，参数={}",ordersSaveParam);
        Orders orders=new Orders();
        BeanUtils.copyProperties(ordersSaveParam,orders);
        // 校验支付金额
        // 修改订单状态
        orders.setStatus("1");
        orders.setPaymentTime(new Date());
        orders.setUpdateTime(new Date());
        ordersMapper.updateOrders(orders);
    }

    @Override
    public List<OrdersVO> selectOrdersByCondition(OrdersQuery ordersQuery) {
        log.debug("用户条件查询订单业务,参数:{}",ordersQuery);
        List<OrdersVO> ordersVOList = ordersMapper.selectOrdersByCondition(ordersQuery);
        return ordersVOList;
    }

    /**
     * 确认下单业务    1、修改订单信息
     *                  已选餐桌、未付款、已付款
     *              2、将该顾客所选分店的购物车内容添加到订单详情表中
     *              3、删除该顾客所选分店的购物车内容
     * @param ordersSaveParam
     */
    @Override
    public void confirmOrder(OrdersSaveParam ordersSaveParam) {
        log.debug("用户点击确认下单业务，参数：{}",ordersSaveParam);
        Long orderId = ordersSaveParam.getId();//订单id
        Long userId = ordersSaveParam.getUserId();//用户id
        Long branchId = ordersSaveParam.getBranchId();//分店id
        OrdersQuery ordersQuery = new OrdersQuery();
        ordersQuery.setId(orderId);
        List<OrdersVO> ordersVOList = ordersMapper.selectOrdersByCondition(ordersQuery);
        String status = ordersVOList.get(0).getStatus();
        log.warn("查询出的订单状态，********************{}",status);
        /*1、修改订单信息*/
        Orders orders = new Orders();
        BeanUtils.copyProperties(ordersSaveParam,orders);//用户id、分店id、餐桌id、订单id

        if(status.equals("2")){
            //1.1如果订单处于已选餐桌状态，第一次点餐
            orders.setStatus("0");//支付状态：未支付
            orders.setUpdateTime(new Date());
            ordersMapper.updateOrder(orders);
        }else if(status.equals("0")){
            //1.2如果订单处于未支付状态，继续点餐
            orders.setTotal(ordersVOList.get(0).getTotal()+ordersSaveParam.getTotal());//原价格+新点的价格
            orders.setUpdateTime(new Date());
            ordersMapper.updateOrder(orders);
        }else if(status.equals("1")){
            //1.3如果订单处于已支付状态，继续点餐，新开一条订单
            Orders ordersNew = new Orders();
            ordersNew.setUserId(ordersSaveParam.getUserId());
            ordersNew.setBranchId(ordersSaveParam.getBranchId());
            ordersNew.setTableId(ordersVOList.get(0).getTableId());
            ordersNew.setStatus("0");
            ordersNew.setDiningStatus("1");
            ordersNew.setTotal(ordersSaveParam.getTotal());
            ordersNew.setRemark("暂无");
            ordersNew.setCreateTime(new Date());
            log.debug("插入之前的订单数据详情，===>>>>{}",ordersNew);
            ordersMapper.insertOrdersCopy(ordersNew);
            log.debug("插入之后的订单数据详情，===>>>>{}",ordersNew);
            orderId = ordersNew.getId();//新订单id
                //把原来的订单用餐状态设置已结束
            orders.setDiningStatus("2");
            orders.setUpdateTime(new Date());
            ordersMapper.updateOrder(orders);
        }

        CartQuery cartQuery = new CartQuery();
        cartQuery.setUserId(userId);
        cartQuery.setBranchId(branchId);
        List<CartVO> cartVOList = cartMapper.select(cartQuery);//购物车列表集合
        for(CartVO cartVO :cartVOList){
            /*2、将该顾客所选分店的购物车内容添加到订单详情表中*/
            DishVODetail dishVODetail = dishMapper.selectDishDetailById(cartVO.getDishId());//菜品详情
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderId(orderId);
            orderDetail.setDish(dishVODetail.getName());
            orderDetail.setDishId(dishVODetail.getId());
            orderDetail.setCuisine(dishVODetail.getTypeName());
            orderDetail.setNumber(cartVO.getNumber());
            orderDetail.setPrice(dishVODetail.getPrice());
            orderDetailMapper.insertOrderDetail(orderDetail);
            /*3、删除该顾客所选分店的购物车内容*/
            cartMapper.deleteById(cartVO.getId());
        }
    }

    @Override
    public void confirmOrderEmp(OrdersSaveParam ordersSaveParam) {
        log.debug("员工点击确认下单业务，参数：{}",ordersSaveParam);
        Long orderId = ordersSaveParam.getId();//订单id
        Long userId = ordersSaveParam.getUserId();//用户id
        Long branchId = ordersSaveParam.getBranchId();//分店id
        OrdersQuery ordersQuery = new OrdersQuery();
        ordersQuery.setId(orderId);
        List<OrdersVO> ordersVOList = ordersMapper.selectOrdersByCondition(ordersQuery);
        String status = ordersVOList.get(0).getStatus();
        /*1、修改订单信息*/
        Orders orders = new Orders();
        BeanUtils.copyProperties(ordersSaveParam,orders);//用户id、分店id、餐桌id、订单id

        // 员工点餐
        //1.1如果订单处于已选餐桌状态，第一次点餐
        orders.setStatus("0");//支付状态：未支付
        orders.setRemark("员工点餐");
        orders.setUpdateTime(new Date());
        ordersMapper.updateOrder(orders);


        CartQuery cartQuery = new CartQuery();
        cartQuery.setUserId(userId);
        cartQuery.setBranchId(branchId);
        List<CartVO> cartVOList = cartMapper.select(cartQuery);//购物车列表集合
        for(CartVO cartVO :cartVOList){
            /*2、将该顾客所选分店的购物车内容添加到订单详情表中*/
            DishVODetail dishVODetail = dishMapper.selectDishDetailById(cartVO.getDishId());//菜品详情
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderId(orderId);
            orderDetail.setDish(dishVODetail.getName());
            orderDetail.setDishId(dishVODetail.getId());
            orderDetail.setCuisine(dishVODetail.getTypeName());
            orderDetail.setNumber(cartVO.getNumber());
            orderDetail.setPrice(dishVODetail.getPrice());
            orderDetailMapper.insertOrderDetail(orderDetail);
            /*3、删除该顾客所选分店的购物车内容*/
            cartMapper.deleteById(cartVO.getId());
        }
    }



    @Override
    public void update(OrdersSaveParam ordersSaveParam) {
        log.debug("释放餐桌业务，参数：{}",ordersSaveParam);
        Orders orders = new Orders();
        BeanUtils.copyProperties(ordersSaveParam,orders);
        ordersMapper.updateOrder(orders);
    }
}
